package com.nq.activaity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.activaity.entity.StUserIntegralRecord;
import com.nq.activaity.service.IStUserIntegralRecordService;
import com.nq.common.ServerResponse;
import com.nq.dao.activaity.StUserIntegralRecordMapper;
import com.nq.pojo.User;
import com.nq.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 用户积分记录表
 * @Author: jeecg-boot
 * @Date:   2024-11-05
 * @Version: V1.0
 */
@Service
public class StUserIntegralRecordServiceImpl extends ServiceImpl<StUserIntegralRecordMapper, StUserIntegralRecord> implements IStUserIntegralRecordService {


    @Resource
    private IUserService userService;

    @Resource
    private StUserIntegralRecordMapper userIntegralRecordMapper;


    public ServerResponse<PageInfo> list(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User user = this.userService.getCurrentRefreshUser(request);
        if (user == null) {
            return ServerResponse.createByErrorMsg("The user does not exist");
        }
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<StUserIntegralRecord> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        recordLambdaQueryWrapper.eq(StUserIntegralRecord::getUserId, user.getId())
                .orderByDesc(StUserIntegralRecord::getCreateTime).orderByDesc(StUserIntegralRecord::getId);
        List<StUserIntegralRecord> recordList = userIntegralRecordMapper.selectList(recordLambdaQueryWrapper);
        recordList.stream().forEach(record -> {
            record.setIntegralNumBefore(record.getIntegralNumBefore().setScale(0));
            record.setIntegralNumAfter(record.getIntegralNumAfter().setScale(0));
            if (record.getIncome().intValue() == 0) {
                record.setIntegralNum(record.getIntegralNum().setScale(0).negate());
            }
        });
        PageInfo pageInfo = new PageInfo(recordList);
        return ServerResponse.createBySuccess(pageInfo);

    }


    /**
     * 获取总积分
     * @param request
     * @return
     */
    @Override
    public ServerResponse getPoints(HttpServletRequest request) {
        User user = this.userService.getCurrentRefreshUser(request);
        if (user == null) {
            return ServerResponse.createByErrorMsg("The user does not exist");
        }
        LambdaQueryWrapper<StUserIntegralRecord> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        recordLambdaQueryWrapper.select(StUserIntegralRecord::getIntegralNum)
                .eq(StUserIntegralRecord::getUserId, user.getId())
                .eq(StUserIntegralRecord::getIncome, 1);
        List<StUserIntegralRecord> recordList = userIntegralRecordMapper.selectList(recordLambdaQueryWrapper);
        BigDecimal totalPoints = recordList.stream().map(StUserIntegralRecord::getIntegralNum).reduce(BigDecimal.ZERO, BigDecimal::add);
        return ServerResponse.createBySuccess(totalPoints.setScale(0));
    }
}
